package pogvue.analysis;


import pogvue.datamodel.*;
import pogvue.io.*;

import java.util.*;
import java.io.*;

public class Main {


    public static void get_mismatches(AlignmentI al,Vector feat) {
	int       window = 20;
	SequenceI refseq = al.getSequenceAt(0);

	for (int i = 1; i < al.getHeight(); i+= 2) {

	    SequenceI seq1 = al.getSequenceAt(i);
	    SequenceI seq2 = al.getSequenceAt(i+1);

	    Vector tmpseqs = new Vector();

	    tmpseqs.addElement(seq1);
	    tmpseqs.addElement(seq2);

	    Vector pid1 = AAFrequency.calculatePID(refseq,tmpseqs,window,0,seq1.getLength());
	    Vector pid2 = AAFrequency.calculatePID(seq1  ,tmpseqs,window,0,seq1.getLength());

	    System.out.println("Size " + pid1.size());

	    for (int j = 0; j < pid1.size(); j++) {
		int overlaps = 0;

		for (int k = 0; k < feat.size(); k++) {
		    SequenceFeature sf = (SequenceFeature)feat.elementAt(k);
		    if (j <= sf.getEnd()-window/2 &&
			j >= sf.getStart()+window/2) {
			overlaps = 1;
			k = feat.size();
		    }
		}
		Vector v1 = (Vector)pid1.elementAt(j);
		Vector v2 = (Vector)pid2.elementAt(j);

		int gaps2 = 0;
		int gaps1 = 0;

		if (j > window/2 && j < pid1.size() - window/2) {
		    gaps1 = countGaps(seq1.getSequence().substring(j-window/2, j+window/2));
		    gaps2 = countGaps(seq2.getSequence().substring(j-window/2, j+window/2));
		}
		// Seq1 org base ucsc_pid_human broad_pid_human broad_pid_ucsc overlaps_featre
		System.out.println("Seq1 " + seq1.getName() + " " + j + " " + v1.elementAt(0) + " " + v1.elementAt(1) + " " + v2.elementAt(1) + " " + overlaps + " " + gaps1 + " " + gaps2);
	    }
	}
    }

    public static int countGaps(String s) {

	char[] tmp = s.toCharArray();

	int gaps = 0;

	for (int i = 0; i < tmp.length; i++) {
	    if (tmp[i] == '-') {
		gaps++;
	    }
	}
	return gaps;
    }

    public static void main(String[] args) {
	try {
	    GappedFastaFile ff = new GappedFastaFile(args[0],"File");
	    GFFFile gff = new GFFFile(args[1],"File");
	    SequenceI[] s = ff.getSeqsAsArray();
	    
	    AlignmentI al = new Alignment(s);
	    
	    Hashtable   fhash  = gff.getFeatureHash();
	    Enumeration en     = fhash.keys();
                
	    while (en.hasMoreElements()) {
		GFF g = (GFF)fhash.get(en.nextElement());
		Vector feat = g.getFeatures();

		Main.get_mismatches(al,feat);
	    }
	} catch (IOException e) {
	    System.out.println("IOException " + e);
	}
    }
}

	
